# -*- coding: utf-8 -*-
# @Time : 2024/1/24 2:28 PM 
# @Author : Wayne
# @Email : 1
# @File : more_group.py
# @Project : young_python_demo
# @DESCRIPTION :

"""
data_list 需要分组的字典数组
fields 需要分组的字段
"""
def group_by_fields(data_list, *fields):
    from collections import defaultdict
    from operator import itemgetter
    result_group = defaultdict(list)
    for item in data_list:
        key = itemgetter(*fields)(item)
        key_str = '-'.join(map(str, key))
        result_group[key_str].append(item)
    return dict(result_group).values()


data = [
    {
        "consignTime": "2024-01-01",
        "shopCode": "s001",
        "warehouseCode":"ck01",
        "googsNO":"商品1"
    },
    {
        "consignTime": "2024-01-01",
        "shopCode": "s001",
        "warehouseCode": "ck01",
"googsNO":"商品2"
    },
    {
        "consignTime": "2024-01-01",
        "shopCode": "s009",
        "warehouseCode": "ck01",
"googsNO":"商品"
    },
    {
        "consignTime": "2024-01-02",
        "shopCode": "s009",
        "warehouseCode": "ck01",
"googsNO":"商品"
    },
]
# 调用分组函数 # 日期、店铺、仓库
data_group = group_by_fields(data, "consignTime", "shopCode", "warehouseCode")
print(data_group)
# dict_values(
# [
# [{'consignTime': '2024-01-01', 'shopCode': 's001', 'warehouseCode': 'ck01', 'googsNO': '商品1'}, {'consignTime': '2024-01-01', 'shopCode': 's001', 'warehouseCode': 'ck01', 'googsNO': '商品2'}],
# [{'consignTime': '2024-01-01', 'shopCode': 's009', 'warehouseCode': 'ck01', 'googsNO': '商品'}, {'consignTime': '2024-01-01', 'shopCode': 's009', 'warehouseCode': 'ck01', 'googsNO': '商品'}]
# ])
